package com.chexiehui.jc.adapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.ActivityUtils;
import com.bumptech.glide.Glide;
import com.chexiehui.jc.R;
import com.chexiehui.jc.entity.HomeBannerInfo;
import com.chexiehui.jc.ui.activity.CarSeriesDetailsActivity;
import com.chexiehui.jc.ui.activity.ForumTribuneDetailsActivity;
import com.chexiehui.jc.ui.activity.WebActivity;
import com.chexiehui.jc.utils.GlideUtil;
import com.chexiehui.jc.utils.NoDoubleClickListener;

import java.util.List;

public class HomeBannerAdapter extends PagerAdapter {

    private List<HomeBannerInfo> dataList;

    public HomeBannerAdapter(List<HomeBannerInfo> imgDataList) {
        this.dataList = imgDataList;
    }

    @Override
    public int getCount() {
        return dataList != null ? dataList.size() : 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(container.getContext()).inflate(R.layout.item_banner, null);
        ImageView iv_item_image = linearLayout.findViewById(R.id.iv_item_image);
        iv_item_image.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                //1：外链 2 ：车系详情 3 论坛详情
                Class clz = null;
                Bundle bundle = null;
                if(dataList.get(position).getType().equals("1")){
                    clz = WebActivity.class;
                    bundle = new Bundle();
                    bundle.putString("url", dataList.get(position).getLink());

                }else if(dataList.get(position).getType().equals("2")){
                    clz = CarSeriesDetailsActivity.class;
                    bundle = new Bundle();
                    bundle.putString("seriesId", dataList.get(position).getLink());
                }else if(dataList.get(position).getType().equals("3")){
                    clz = ForumTribuneDetailsActivity.class;
                    bundle = new Bundle();
                    bundle.putString("forumId", dataList.get(position).getLink());
                }
                if(clz != null){
                    Intent intent = new Intent(container.getContext(), clz);
                    intent.putExtras(bundle);
                    ActivityUtils.startActivity(intent, R.anim.slide_in_from_right, R.anim.slide_out_to_left);
                }


            }
        });
        GlideUtil.loadImageViewCrop(container.getContext(), dataList.get(position).getImage(), iv_item_image, R.mipmap.ic_logo_2_b, R.mipmap.ic_logo_2_b);
        container.addView(linearLayout);
        return linearLayout;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        LinearLayout view = (LinearLayout) object;
        container.removeView(view);
    }

    public List<HomeBannerInfo> getDataList() {
        return dataList;
    }
}
